<template>
  <div class="dialog-container">
    <el-dialog
      title="模板管理"
      :visible="dialogVisible"
      width="60%"
      :close-on-click-modal="false"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType === 'detail'"
      >
        <!-- 自定义表格内容 -->
        <div class="custom-table-panel">
          <component :is="flag" ref="form2" :obj="dataForm" :maintena-index="maintenaIndex" />
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import Jy from './form/jy'
import Wh from './form/wh'
import Pjgh from './form/pjgh'
import Xj from './form/xj'
import Fyhs from './form/fyhs'
import Bd from './form/bd'
import Sjgh from './form/sjgh'
import Yqxj from './form/yqxj'
import Yqwh from './form/yqwh'
import Yqgh from './form/yqgh'
import Yqjz from './form/yqjz'
import Yqbd from './form/yqbd'
import { getById } from '@/api/business/template'
export default {
  name: 'AddTemplateDialog',
  components: {
    Wh, Pjgh, Xj, Yqxj, Yqwh, Yqgh, Yqjz, Yqbd, Jy, Fyhs, Bd, Sjgh
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    obj: {
      type: Object,
      default: null
    },
    formDetailsId: {
      type: String,
      default: ''
    },
    maintenaIndex: {
      type: Number,
      default: 0
    },
    outletData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {},
      flag: null,
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  created() {

  },
  methods: {
    initData() {
      if (this.obj) {
        if (this.outletData.formTempId) {
        // 查询模板
          getById(this.outletData.formTempId).then(res => {
            if (res.data.obj) {
              this.flag = res.data.obj.modelUrl
            }
            this.dataForm = this.obj
            if (this.$refs.form2) {
              this.$refs.form2.init()
            }
          })
        }
      }
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const data = this.$refs.form2.getFormData()
          data.maintenaIndex = this.maintenaIndex
          data.id = this.formDetailsId
          this.$emit('saveMaintena', data)
          this.closeDialog()
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
      this.$refs['form'].clearValidate()
    },
    clearData() {
      // this.$refs.form2.clearData()
      this.dataForm = {}
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
